package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.AttrKey;
import com.atguigu.lease.model.entity.AttrValue;
import com.atguigu.lease.model.entity.FeeValue;
import com.atguigu.lease.web.admin.service.AttrKeyService;
import com.atguigu.lease.web.admin.service.AttrValueService;
import com.atguigu.lease.web.admin.vo.attr.AttrKeyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "房间属性管理")
@RestController
@RequestMapping("/admin/attr")
public class AttrController {
@Autowired
private AttrKeyService attrKeyService;
@Autowired
private AttrValueService attrValueService;
    @Operation(summary = "新增或更新属性名称")
    @PostMapping("key/saveOrUpdate")
    public Result saveOrUpdateAttrKey(@RequestBody AttrKey attrKey) {
        boolean b = attrKeyService.saveOrUpdate(attrKey);
        if (!b) {
            return Result.fail();
        }
        return Result.ok();
    }

    @Operation(summary = "新增或更新属性值")
    @PostMapping("value/saveOrUpdate")
    public Result saveOrUpdateAttrValue(@RequestBody AttrValue attrValue) {
        boolean b = attrValueService.saveOrUpdate(attrValue);
        if (!b) {
            return Result.fail();
        }
        return Result.ok();
    }


    @Operation(summary = "查询全部属性名称和属性值列表")
    @GetMapping("list")
    public Result<List<AttrKeyVo>> listAttrInfo() {
        List<AttrKeyVo> list=attrKeyService.costomList();
        return Result.ok(list);
    }

    /**
 * 根据属性键ID删除属性名称
 * 此方法首先删除与给定属性键ID关联的所有属性值，然后删除属性键本身
 * 如果删除成功，返回表示操作成功的Result对象；如果删除失败，返回表示操作失败的Result对象
 *
 * @param attrKeyId 要删除的属性键的ID
 * @return 删除操作的结果，包含操作状态和可能的错误信息
 */
@Operation(summary = "根据id删除属性名称")
@DeleteMapping("key/deleteById")
public Result removeAttrKeyById(@RequestParam Long attrKeyId) {
    System.out.println("attrKeyId = " + attrKeyId);
    // 创建查询包装器以查找和删除具有给定属性键ID的所有属性值
    LambdaQueryWrapper<AttrValue> wrapper=new LambdaQueryWrapper<>();
    wrapper.eq(AttrValue::getAttrKeyId,attrKeyId);
    // 删除与给定ID关联的所有属性值
    attrValueService.remove(wrapper);
    // 删除属性键本身
    boolean b = attrKeyService.removeById(attrKeyId);
    // 根据删除操作的结果返回相应的Result对象
    if (!b) {
        return Result.fail();
    }
    return Result.ok();
}


    @Operation(summary = "根据id删除属性值")
    @DeleteMapping("value/deleteById")
    public Result removeAttrValueById(@RequestParam Long id) {
        boolean b = attrValueService.removeById(id);
        if (!b) {
            return Result.fail();
        }
        return Result.ok();
    }

}
